package io.github.uicdb.badskill;

import io.github.uicdb.badskill.api.CostumerConsumer;
import io.github.uicdb.badskill.api.CostumerConsumerManager;
import io.github.uicdb.badskill.network.AllOnlySyncPackage;
import io.github.uicdb.badskill.network.NetWorking;
import io.github.uicdb.badskill.network.OnlySyncPackage;
import io.github.uicdb.badskill.network.PlayerLearnedSyncPackage;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.ListTag;
import net.minecraft.nbt.StringTag;
import net.minecraft.nbt.Tag;
import net.minecraft.network.chat.Component;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.entity.player.Player;
import net.minecraftforge.common.capabilities.AutoRegisterCapability;
import net.minecraftforge.common.util.INBTSerializable;
import net.minecraftforge.network.PacketDistributor;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;

@Data
@AutoRegisterCapability
public class OnlyCapability implements CostumerConsumer , INBTSerializable<ListTag>,ICapabilityCoppier {
    private final Player player;
    private final List<String> existKeys=new ArrayList<>();
    public void removeKey(String key){existKeys.remove(key);}
    public void addKey(String key){if(!existKeys.contains(key))existKeys.add(key);}
    @Override
    public boolean consume(Player player, String key, double amounts, SkillOption stack, boolean simulate) {
        if(!simulate){
            if(!existKeys.contains(key)) {
                existKeys.add(key);
                if(player instanceof ServerPlayer serverPlayer){
                    NetWorking.simpleChannel.send(PacketDistributor.PLAYER.with(()->serverPlayer), OnlySyncPackage.create(key,true));
                }
                return true;
            }
        }
        return !existKeys.contains(key);
    }

    @Override
    public boolean retreated(Player player, String key, double amounts, SkillOption stack, boolean backResources) {
        if (existKeys.contains(key)) {
            existKeys.remove(key);
            if(player instanceof ServerPlayer serverPlayer){
                NetWorking.simpleChannel.send(PacketDistributor.PLAYER.with(()->serverPlayer), OnlySyncPackage.create(key,false));
            }
            return true;
        }
        return false;
    }

    @Override
    public void enable(Player player, String key, double amounts, SkillOption stack) {

    }

    @Override
    public List<Component> getTooltips(Player player, String key, double amounts, SkillOption stack, boolean isRequirements) {
        if(isRequirements)return List.of(Component.empty().append(Component.translatable("tips.badskill.only."+key+".desc")));
        return List.of();
    }

    @Override
    public void disable(Player player, String key, double data, SkillOption option) {

    }

    @Override
    public ListTag serializeNBT() {
        ListTag listTag=new ListTag();
        existKeys.forEach(e->listTag.add(StringTag.valueOf(e)));
        return listTag;
    }

    @Override
    public void deserializeNBT(ListTag nbt) {
        existKeys.clear();
        nbt.forEach(tag -> existKeys.add(tag.getAsString()));
    }

    @Override
    public void beforeCopyCapability() {

    }

    @Override
    public void afterCopyCapability() {
        if (player instanceof ServerPlayer serverPlayer) {
            NetWorking.simpleChannel.send(PacketDistributor.PLAYER.with(() -> serverPlayer),new AllOnlySyncPackage(existKeys));
        }
    }
}
